
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@SuppressWarnings("serial")
public class InitParamTestServlet extends HttpServlet {
	Connection conn;

	public void init(javax.servlet.ServletConfig config)
			throws javax.servlet.ServletException {
		
		String url = config.getInitParameter("url");
		String id = config.getInitParameter("id");
		String pw = config.getInitParameter("pw");
		
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection(
					url, id, pw);
		} catch (Exception e) {
			System.out.println("DB 접속 오류 발생 :" + e);
		}
	}

	public void doGet(HttpServletRequest req, HttpServletResponse res) {
		PrintWriter out = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			res.setContentType("text/html; charset=EUC-KR");
			out = res.getWriter();
			out.println("<html>");
			out.println("<head><title>Request 정보 출력 Servlet</title></head>");
			out.println("<body>");
			stmt = conn.createStatement();
			rs = stmt.executeQuery("SELECT * from product");
			out.println("<h3>product 테이블의 내용</h3>");
			out.println("<pre>");
			while (rs.next()) {
				out.print(rs.getString("id") + "   ");
				out.print(rs.getString("classid") + "   ");
				out.print(rs.getString("name") + "   ");
				out.print(rs.getInt("balance") + "   ");
				out.println(rs.getFloat("price"));
			}
			out.println("</pre>");
			out.println("</body></html>");
			out.close();
		} catch (Exception e) {
			out.println("<h3>요청을 처리하는 동안 오류가 발생했습니다!!!</h3>");
			out.println("</body></html>");
			out.close();
			System.out.println("오류 : " + e);
		} finally {
			try {
				if (stmt != null)
					stmt.close();
				if (rs != null)
					rs.close();
			} catch (Exception e) {
				System.out.println("DB 자원 해제 오류 발생 :" + e);
			}
		}
	}

	public void destroy() {
		try {
			if (conn != null)
				conn.close();
		} catch (Exception e) {
			System.out.println("DB 접속 해제 오류 발생 :" + e);
		}
	}
}
